最近项目上需要做轨迹回放的功能,所以就着手去研究了,高德地图SDK是有点的移动的接口的,但是没有对播放暂停,加速减速进行封装,需要我们自己去自定义;

IMG_0679.jpg

直接上代码:
JVMoveAnimation.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface JVMoveAnimation : NSObject

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, copy) NSArray *tracks; //坐标集数据源
/*
减速滑动
*/
- (void)slowdown;
/*
开始滑动或者暂停滑动
*/
- (void)play:(BOOL)play cmpleteCallback:(void(^)(BOOL isFinished))completeCallback;
/*
加速滑动
*/
- (void)speedUp;
@end

NS_ASSUME_NONNULL_END

JVMoveAnimation.m 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#import "JVMoveAnimation.h"


@interface JVMoveAnimation ()
///暂停时所滑动的点
@property (nonatomic, assign) NSInteger suspendedCount;

///动画时长
@property (nonatomic, assign) CGFloat duration;
///速度 加减速
@property (nonatomic, assign) NSInteger speed;
///滑动的点数据集
@property (nonatomic, strong) NSMutableArray *animatedTracks;

@property (nonatomic, strong) MAAnimatedAnnotation* annotation;

@property (nonatomic, copy) void (^completeCallback)(BOOL isFinished);
@end
@implementation JVMoveAnimation


- (void)slowdown
{

if (self.annotation) {
NSArray *annos = [self.annotation allMoveAnimations];
for (MAAnnotationMoveAnimation *anno in annos) {
self.speed-=1;
self.suspendedCount = [anno passedPointCount]+1;
}
// [HUBManager showItoastMsg:@" 速度-5 " hide:YES afterDelay:1];
}
}


- (void)play:(BOOL)play cmpleteCallback:(void(^)(BOOL isFinished))completeCallback
{

self.completeCallback = completeCallback;
if (play) {
[self palyAnimated];
}
else //暂停
{
NSArray *annos = [self.annotation allMoveAnimations];
for (MAAnnotationMoveAnimation *anno in annos) {
//当前这个点要移动完成才暂停,所以移动的点数需要+1
self.suspendedCount = [anno passedPointCount]+1;
}

return;
}
}

- (void)speedUp
{

if (self.annotation) {
NSArray *annos = [self.annotation allMoveAnimations];
for (MAAnnotationMoveAnimation *anno in annos) {
self.speed+=1;
self.suspendedCount = [anno passedPointCount]+1;
}

// [HUBManager showItoastMsg:@" 速度+5 " hide:YES afterDelay:1];
}

}

#pragma mark ----------- 播放动画 -------------
- (void)palyAnimated
{
if (self.annotation) {
[self.mapView removeAnnotation:self.annotation];
self.annotation = nil;
}

NSInteger count;

if (self.suspendedCount) { ///暂停再播放

for (int i = 0; i < self.suspendedCount-1; i++) {
[self.animatedTracks removeObjectAtIndex:0]; //移除数据集中已经滑动的点
}
count = self.animatedTracks.count;
self.duration = self.duration - self.duration * (self.speed * 0.05);
self.suspendedCount = 0;
self.speed = 0;
}
else
{
self.animatedTracks = [NSMutableArray arrayWithArray:self.tracks];
count = self.animatedTracks.count;
self.duration = count*2;
}


CLLocationCoordinate2D coords[count];
for (int i = 0; i < count; i++) {
Track *track = self.animatedTracks[I];
coords[i].latitude = track.lat;
coords[i].longitude = track.lon;
}


MAAnimatedAnnotation *anno = [[MAAnimatedAnnotation alloc] init];
anno.coordinate = coords[0];
self.annotation = anno;

[self.mapView addAnnotation:self.annotation];
WEAKSELF(weakSelf);
[anno addMoveAnimationWithKeyCoordinates:coords count:count withDuration:self.duration withName:nil completeCallback:^(BOOL isFinished) {
if (isFinished) {
if (weakSelf.completeCallback) {
weakSelf.completeCallback(isFinished);
}
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.mapView removeAnnotation:weakSelf.annotation];
weakSelf.annotation = nil;
});
}
} stepCallback:^(MAAnnotationMoveAnimation *currentAni) {
if (currentAni.passedPointCount == weakSelf.suspendedCount) {
[currentAni cancel]; //取消动画

if (weakSelf.speed != 0) { ///速度改变后,重新添加滑动动画
weakSelf.duration = currentAni.duration - currentAni.elapsedTime; //计算剩余的时间
[weakSelf palyAnimated]; //重新开启动画
}
}

}];
}

@end
加减速的值可以根据自己的需求改;
可能有考虑不全面的地方,但是整个实现的思路方向应该是没有问题的